import http from "@/utils/fetch";

//通用导出接口
export default function download(urlParams,data){
  const {menuCode,menuListCode,name:excelName} = urlParams;
  return http.post(`/listConfigExport/${menuCode}/${menuListCode}/${excelName}`,data,{responseType: 'arraybuffer'}).then(response => {
    if(response.headers && response.headers["content-type"].indexOf("application/vnd.ms-excel") > -1){
      if (window.navigator.msSaveBlob) {
        var blobObject = new Blob([response.data]);
        window.navigator.msSaveBlob(blobObject, `${excelName}.xls`);
        return Promise.resolve(response)
      } else {
          var blob = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
          var filename = `${excelName}.xls`;
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
          return Promise.resolve(response)
      }
    }
  })
}